﻿@typeparam TItem
@inherits _BaseDataGridRowEdit<TItem>
@if ( EditMode == DataGridEditMode.Form )
{
    <Validations @ref="validations" Mode="ValidationMode.Manual" StatusChanged="@ValidationsStatusChanged" Model="@ValidationItem">
        <TableRow>
            <TableRowCell ColumnSpan="@(Columns.Count() + 1)">
                @if ( ParentDataGrid.ShowValidationsSummary )
                {
                    <Row Display="@(ParentDataGrid.HasValidationsSummary || isInvalid ? Display.Always : Display.None)">
                        <ValidationSummary Label="@ParentDataGrid.ValidationsSummaryLabel" Errors="@ParentDataGrid.ValidationsSummaryErrors" />
                    </Row>
                }
                <Row>
                    <Column>
                        <Fields>
                            @foreach ( var column in OrderedEditableColumns )
                            {
                                <Field ColumnSize="ColumnSize.IsHalf.OnDesktop" Padding="Blazorise.Padding.Is2.OnX.Is2.OnY">
                                    <FieldLabel>
                                        @column.Caption
                                    </FieldLabel>
                                    <FieldBody>
                                        <_DataGridCell TItem="TItem" Column="@column" Item="@Item" ValidationItem="@ValidationItem" CellEditContext="@CellValues[column.ElementId]" ShowValidationFeedback="@ParentDataGrid.ShowValidationFeedback" />
                                    </FieldBody>
                                </Field>
                            }
                        </Fields>
                    </Column>
                </Row>
                <Row>
                    <Column>
                        @{
                            var saveButtonString = Localizer.Localize( ParentDataGrid.Localizers?.SaveButtonLocalizer, "Save" );

                            @if ( ParentDataGrid.CommandColumn?.SaveCommandTemplate != null )
                            {
                                @ParentDataGrid.CommandColumn.SaveCommandTemplate( new()
                                {
                                    Clicked = callbackFactory.Create( this, SaveWithValidation ),
                                    LocalizationString = saveButtonString,
                                    Item = Item,
                                } );
                            }
                            else
                            {
                                @if ( ParentDataGrid.SubmitFormOnEnter )
                                {
                                    <Button Type="ButtonType.Submit" PreventDefaultOnSubmit Color="Color.Link" Clicked="@SaveWithValidation">
                                        @saveButtonString
                                    </Button>
                                }
                                else
                                {
                                    <Button Color="Color.Link" Clicked="@SaveWithValidation">
                                        @saveButtonString
                                    </Button>
                                }
                            }

                            var cancelButtonString = Localizer.Localize( ParentDataGrid.Localizers?.CancelButtonLocalizer, "Cancel" );

                            @if ( ParentDataGrid.CommandColumn?.CancelCommandTemplate != null )
                            {
                                @ParentDataGrid.CommandColumn.CancelCommandTemplate( new()
                                {
                                    Clicked = Cancel,
                                    LocalizationString = cancelButtonString,
                                    Item = Item,
                                } );
                            }
                            else
                            {
                                <Button Color="Color.Link" Clicked="@Cancel">
                                    @cancelButtonString
                                </Button>
                            }
                        }
                    </Column>
                </Row>
            </TableRowCell>
        </TableRow>
    </Validations>
}
else if ( EditMode == DataGridEditMode.Inline )
{
    <Validations @ref="validations" Mode="ValidationMode.Manual" StatusChanged="@ValidationsStatusChanged" Model="@ValidationItem">
        @if ( ParentDataGrid.ShowValidationsSummary )
        {
            <TableRow Display="@(ParentDataGrid.HasValidationsSummary || isInvalid ? Display.TableRow : Display.None)">
                <TableRowCell ColumnSpan="@Columns.Count()">
                    <ValidationSummary Label="@ParentDataGrid.ValidationsSummaryLabel" Errors="@ParentDataGrid.ValidationsSummaryErrors" />
                </TableRowCell>
            </TableRow>
        }
        <TableRow>
            @foreach ( var column in DisplayableColumns )
            {
                @if ( column.IsCommandColumn )
                {
                    @if ( ParentDataGrid.IsCommandVisible )
                    {
                        <_DataGridRowCommand TItem="TItem" Item="@Item" EditState="DataGridEditState.Edit" Column="column" Save="@SaveWithValidation" />
                    }
                }
                else if ( column.IsMultiSelectColumn )
                {
                    @if ( ParentDataGrid.MultiSelect )
                    {
                        <TableRowCell Class="@column.CellClass?.Invoke(Item)"
                                      Style="@column.BuildCellStyle(Item)"
                                      TextAlignment="@column.TextAlignment"
                                      VerticalAlignment="@column.VerticalAlignment"
                                      Display="@column.Display"></TableRowCell>
                    }
                }
                else if ( column.Displayable )
                {
                    <TableRowCell Class="@column.CellClass?.Invoke(Item)" Style="@column.BuildCellStyle(Item)" TextAlignment="@column.TextAlignment" VerticalAlignment="@column.VerticalAlignment" Display="@column.Display">
                        @if ( ParentDataGrid.IsCommandVisible && column.CellValueIsEditable )
                        {
                            <_DataGridCell TItem="TItem" Column="@column" Item="@Item" ValidationItem="@ValidationItem" CellEditContext="@CellValues[column.ElementId]" ShowValidationFeedback="@ParentDataGrid.ShowValidationFeedback" />
                        }
                        else
                        {
                            if ( column.DisplayTemplate != null )
                            {
                                @column.DisplayTemplate( Item )
                            }
                            else
                            {
                                @column.FormatDisplayValue( Item )
                            }
                        }
                    </TableRowCell>
                }
            }
        </TableRow>
    </Validations>
}